﻿/******************************************************************************
 * 【本类功能概述】                                 					      *
 *  版权所有（C）2017-20XX                                                    *
 *  保留所有权利。                                                            *
 ******************************************************************************
 *  作者 : <hughjk>
 *  创建时间: 2019/9/16 16:18:38
 *  文件描述:状态管理类
 *****************************************************************************/

using System.Collections.Generic;
using UnityEngine;
using Util;

namespace PhasesModel
{
    public class PhasesManager : ModelClueInfo
    {
        [HideInInspector] public Dictionary<PhasesType, Phases> PhasesAll = new Dictionary<PhasesType, Phases>();
        [HideInInspector] public Phases[] PhasesSlots;
        [HideInInspector] public Transform PhasesRoot;
        public Phases CurPhases;
        public Phases UpPhases;
        public bool isDebug = false;

        private bool _isPause;

        public override void InitModule(GameManage Character)
        {
            base.InitModule(Character);
            PhasesRoot = transform;
            CreationPhases();
            PhasesSlots = PhasesRoot.GetComponentsInChildren<Phases>();
            for (int i = 0; i < PhasesSlots.Length; i++)
            {
                var s = PhasesSlots[i];
                s.InitModule(Character);
                if (PhasesAll.ContainsKey(s.PhasesType))
                {
                    debug.log(debug.Level.error_3, "状态:", s.name, " 类型:", s.PhasesType, "设置错误!!! 状态初始化需要在 InitModule() 完成");
                }
                PhasesAll.Add(s.PhasesType, s);
                if (s.PhasesType == PhasesType.Idle)
                {
                    CurPhases = s;
                }
            }
            if (CurPhases.isNull())
            {
                debug.log(debug.Level.error_3, transform.name, "状态机没有默认节点!!!");
            }
            UpPhases = CurPhases;
        }

        public override void GetModule()
        {
            for (int i = 0; i < PhasesSlots.Length; i++)
            {
                PhasesSlots[i].GetModule();
            }
            //进入默认状态
            CurPhases.Starts();
        }

        public void Updata(float dt)
        {
            if (_isPause)
            {
                return;
            }
            CurPhases.Reason(dt);
            CurPhases.Updates(dt);
        }

        public void SwitchPhases(PhasesType type)
        {
            debug.log(isDebug, CurPhases.PhasesType, "to", type);
            if (CurPhases.PhasesType != type)
            {
                CurPhases.Ends();
                UpPhases = CurPhases;
                CurPhases = PhasesAll[type];
                CurPhases.Starts();
            }
        }

        public virtual T FinPhases<T>(PhasesType types) where T : Phases
        {
            Phases Phases;
            if (!PhasesAll.TryGetValue(types, out Phases))
            {
                return default(T);
            }
            return Phases as T;
        }

        public virtual T FinOrAddPhases<T>(PhasesType types) where T : Phases
        {
            Phases Phases;
            if (!PhasesAll.TryGetValue(types, out Phases))
            {
                Phases = PhasesRoot.FindOrNew(typeof(T).Name).GetOrAddComponent<T>();
                Phases.InitModule(m_GameManage);
                PhasesAll.Add(types, Phases);
                PhasesSlots = new List<Phases>(PhasesAll.Values).ToArray();
            }
            return Phases as T;
        }

        [ContextMenu("CreationPhases")]
        public void CreationPhases()
        {
            //PhasesRoot = transform.FindOrNew("Phases");
            //PhasesRoot.FindOrNew<IdlePhases>().PhasesType = PhasesType.Idle;
            //PhasesRoot.FindOrNew<CarActivatePhases>().PhasesType = PhasesType.Activated;
            //PhasesRoot.FindOrNew<CarUnActivatePhases>().PhasesType = PhasesType.Unactivated;
            //PhasesRoot.FindOrNew<CarSteerPhases>().PhasesType = PhasesType.Steer;
        }

        public void Pause()
        {
            _isPause = true;
        }

        public void Resume()
        {
            _isPause = false;
        }
    }
}